ihelpuoo.namespace("ihelpuoo.widget.date");

var ihelpuoo_widget_datepickers = null;

ihelpuoo.widget.date.hideAllDatePicker = function()
{
	for(var i=0;i<ihelpuoo_widget_datepickers.length;i++)
	{
		var vn = ihelpuoo_widget_datepickers[i];
		var v = ihelpuoo.widget.getView(vn);
		v.hidePicker();
	}
}

ihelpuoo.widget.date.DatePicker = function(gasket,name,patternName,dv)
{
	if(!name)
	{
		ihelpuoo.error("You must define a unique name for DatePicker!");
		return;
	}
	if(!ihelpuoo_widget_datepickers)
	{
		ihelpuoo_widget_datepickers = new Array();
		ihelpuoo.event.add(document.body,"click",ihelpuoo.widget.date.hideAllDatePicker);
	}
	this.name = name;
	this.view = new ihelpuoo.widget.View(gasket
										,(patternName?patternName:"eve.pattern.date.Picker")
										,this.name);
	if(dv)
		this.vDate = dv;
	else
		this.vDate = new ihelpuoo.widget.date.DateView(this.onChangeDate
													,null
													,this.name+".vDate"
													,null
													,this.onDbClick);
	
	this.vDate.parent = this;
	
	this.oPicker = null;
	this.myDate = null;
	ihelpuoo.widget.registerView2(this);
	ihelpuoo_widget_datepickers.push(this.name);
}

ihelpuoo.widget.date.DatePicker.prototype
=
{
	draw: function(date)
	{
		this.myDate = date;
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oPicker = this.view.getElement("picker");
			this.vDate.setGasket(this.oPicker);
			this.view.addEvent("show","click",this.onShowPicker);
			this.view.addEvent("clear","click",this.onClear);			
		}
		this.view.display();
		if(date)
		{
			this.view.set("box",date.toString());
			this.oPicker.style.disyplay="none";
			this.vDate.draw(date);
			this.vDate.hide();
			this.oPicker.style.disyplay="block";
		}
		else
		{
			this.view.set("box","");
			var d = new Date();
			this.vDate.date = new ihelpuoo.util.Date(d.getFullYear(),d.getMonth()+1,d.getDate());
			
			this.oPicker.style.disyplay="none";
			this.vDate.draw();
			this.vDate.hide();
			this.oPicker.style.disyplay="block";
		}
	}
	
	,
	onClear: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.clear();
	}
	
	,
	clear: function()
	{
		this.draw();
	}
	
	,
	onShowPicker: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		ihelpuoo.widget.date.hideAllDatePicker();
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.showPicker();
	}
	
	,
	showPicker: function()
	{
		this.vDate.display();
		this.oPicker.style.zIndex = 1000;
		this.oPicker.style.display = "block";
	}
	
	,
	hidePicker: function()
	{
		this.oPicker.style.display = "none";
	}
	
	,
	onChangeDate: function(dv)
	{
		if(!dv)return;
		dv.parent.view.set("box",dv.getDate().toString());
		dv.parent.myDate = dv.getDate();
	}
	
	,
	onDbClick: function(dv)
	{
		dv.hide();
	}
	
	,
	getDate: function()
	{
		return this.myDate;
	}
		
	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,display: function(){this.view.display();}
	,hide: function(){this.view.hide();}
	// </WEBOO:INHERIT>
}